package com.chx.controller;

import com.chx.dto.PurchaseOrderDto;
import com.chx.dto.PurchaseOrderPageQueryDto;
import com.chx.dto.StockInOrderDto;
import com.chx.result.PageResult;
import com.chx.result.Result;
import com.chx.service.PurchaseOrderService;
import com.chx.vo.PurchaseOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author cp-月入过万
 * @Date 2025/5/30
 * 采购订单控制器
 */
@RestController
@RequestMapping("/admin/purchase-order")
@Api(tags = "采购订单相关接口")
@Slf4j
public class PurchaseOrderController {
    @Autowired
    private PurchaseOrderService purchaseOrderService;
    /**
     * 新增采购订单
     * @param purchaseOrderDto
     * @return
     */
    @ApiOperation(value = "新增采购订单")
    @PostMapping
    public Result<String> save(@RequestBody PurchaseOrderDto purchaseOrderDto){
        log.info("新增采购订单:{}",  purchaseOrderDto);
        purchaseOrderService.save(purchaseOrderDto);
        return Result.success();
    }
    /**
     * 修改采购订单
     * @param purchaseOrderDto
     */
    @ApiOperation(value = "修改采购订单")
    @PutMapping
    public Result<String> update(@RequestBody PurchaseOrderDto purchaseOrderDto){
        log.info("修改采购订单:{}",  purchaseOrderDto);
        purchaseOrderService.update(purchaseOrderDto);
        return Result.success();
    }
    /**
     * 取消采购订单
     */
    @ApiOperation(value = "取消采购订单")
    @PutMapping("/status/cancel/{id}")
    public Result<String> cancel(@PathVariable Long id){
        log.info("取消采购订单:{}",  id);
        purchaseOrderService.cancel(id);
        return Result.success();
    }
    /**
     * 根据id获取采购订单详情
     */
    @ApiOperation(value = "根据id获取采购订单详情")
    @GetMapping("/{id}")
    public Result<PurchaseOrderVo> getById(@PathVariable Long id){
        log.info("根据id获取采购订单详情:{}",  id);
        return Result.success(purchaseOrderService.getById(id));
    }
    /**
     * 分页查询采购订单
     */
    @ApiOperation(value = "分页查询采购订单")
    @GetMapping("/page")
    public Result<PageResult> page(PurchaseOrderPageQueryDto purchaseOrderPageQueryDto){
        log.info("分页查询采购订单:{}",  purchaseOrderPageQueryDto);
        return Result.success(purchaseOrderService.page(purchaseOrderPageQueryDto));
    }
    /**
     * 审核订单
     */
    @ApiOperation(value = "审核订单")
    @PutMapping("/status/{id}")
    public Result<String> updateStatus(@PathVariable Long id){
        log.info("审核订单状态：{}", id);
        purchaseOrderService.updateStatus(id);
        return Result.success();
    }
    /**
     * 提交订单
     */
    @ApiOperation(value = "提交订单")
    @PostMapping("/status")
    public Result<String> commit(@RequestBody StockInOrderDto stockInOrderDto){
        log.info("提交订单：{}", stockInOrderDto);
        purchaseOrderService.commit(stockInOrderDto);
        return Result.success();
    }
}
